package com.itheima.netty.http;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.stream.ChunkedWriteHandler;

public class HttpSnoopClientInitializer extends ChannelInitializer<SocketChannel> {

	private final SslContext sslCtx;

	public HttpSnoopClientInitializer(SslContext sslCtx) {
		this.sslCtx = sslCtx;
	}

	@Override
	public void initChannel(SocketChannel ch) {
		ChannelPipeline p = ch.pipeline();

		// Enable HTTPS if necessary.
		if (sslCtx != null) {
			p.addLast(sslCtx.newHandler(ch.alloc()));
		}

		p.addLast(new HttpClientCodec());

		// Remove the following line if you don't want automatic content
		// decompression.
		p.addLast(new HttpContentDecompressor());

		// Uncomment the following line if you don't want to handle
		// HttpContents.
		p.addLast(new HttpObjectAggregator(1048576));
		p.addLast(new ChunkedWriteHandler());
		p.addLast(new HttpSnoopClientHandler());
	}
}